package Model;

import java.util.ArrayList;
import java.util.Observable;
import java.util.Random;
/**
 * 
 * @author KOK Leng-Steven & RIEU Simon
 */
public class Grid extends Observable
{

    private int size = 20;
    private Avatar avatar;
    private ArrayList<ByStander> byStanders;
    private int numberOfByStanders = 8;

    public Grid(int length, int numberOfByStanders)
    {
        this.size = length;
        this.numberOfByStanders = numberOfByStanders;
        this.byStanders = new ArrayList<ByStander>(this.numberOfByStanders);
        
          //Generate the position of the Avatar
        this.avatar = new Avatar(new Character(this.generateRandomNumberWithinARange(0,this.size),
                this.generateRandomNumberWithinARange(0,this.size),7));
       
        //Generate the positions of the byStanders
        for (int i = 0; i < this.numberOfByStanders; i++)
        {
            ByStander b = new ByStander(this.generateRandomNumberWithinARange(0,this.size),
                    this.generateRandomNumberWithinARange(0,this.size) ,
                    this.generateRandomNumberWithinARange(3,7),this.avatar);

            while (this.byStanders.contains(b) || 
                    (b.getI() == avatar.getI() && b.getJ() == avatar.getJ()))
            {
                b.setIJ(this.generateRandomNumberWithinARange(0,this.size),
                        this.generateRandomNumberWithinARange(0,this.size));

            }
            this.byStanders.add(b);
        }

      System.out.println("Avatar");
      System.out.println("Cordonnée en x:"+avatar.getI());
      System.out.println("Cordonnée en y:"+avatar.getJ());
      System.out.println("Champs de vision:"+avatar.getViewField());
      System.out.println("---------");
                                       

        for (int i = 0; i < this.byStanders.size(); i++)
        {
                              System.out.println("Passant "+i);

                  System.out.println("Cordonnée en x:"+this.byStanders.get(i).getI());
                  System.out.println("Cordonnée en x:"+this.byStanders.get(i).getJ());
                  System.out.println("Champ de vision"+this.byStanders.get(i).getViewField());

                  System.out.println("---------");
        }
        //notify the views
        this.setChanged();
        this.notifyObservers();  
    }

    public int getSize()
    {
        return size;
    }

    public void setSize(int size)
    {
        this.size = size;
    }

    public Avatar getAvatar()
    {
        return avatar;
    }

    public void setAvatar(Avatar avatar)
    {
        this.avatar = avatar;
    }
    
    public void moveAvatar(int i, int j)
    {
        this.avatar.setIJ(i, j);
        
        //notify the views
        this.setChanged();
        this.notifyObservers();
    }

    public ArrayList<ByStander> getByStanders()
    {
        return byStanders;
    }

    public void setByStanders(ArrayList<ByStander> byStanders)
    {
        this.byStanders = byStanders;
    }

    public int getNumberOfByStanders()
    {
        return numberOfByStanders;
    }

    public void setNumberOfByStanders(int numberOfByStanders)
    {
        this.numberOfByStanders = numberOfByStanders;
    }

    private int generateRandomNumberWithinARange(int min, int max)
    {
        Random r = new Random();
        return r.nextInt(max-min)+min;
    }

    public Grid()
    {
        this(20,8);
    }
}
